from components.config import WFQ_SOURCE_MYSQL_CONFIG
from utils.db.mysqldb import MysqlDB


class CaptchaSave:
    _to_db = None

    @property
    def to_db(self):
        if not self._to_db:
            self._to_db = MysqlDB(
                ip=WFQ_SOURCE_MYSQL_CONFIG["MYSQL_IP"],
                port=WFQ_SOURCE_MYSQL_CONFIG["MYSQL_PORT"],
                db=WFQ_SOURCE_MYSQL_CONFIG["MYSQL_DB"],
                user_name=WFQ_SOURCE_MYSQL_CONFIG["MYSQL_USER_NAME"],
                user_pass=WFQ_SOURCE_MYSQL_CONFIG["MYSQL_USER_PASS"],
            )
        return self._to_db

    def upload_captcha_to_mysql(self, upload_data):
        self.to_db.add_smart(
            "captcha_geetest_answer", upload_data, update_columns=list(upload_data.keys()) + ["update_time"]
        )

    def get_points_by_history(self, uk_md5):
        datas = self.to_db.find(
            f"""
            SELECT * FROM captcha_geetest_answer
            WHERE uk_md5 = '{uk_md5}'
            """,
            to_json=True,
        )
        if datas:
            print(f"查询到历史 {datas}")
            return datas[0]["answer"]
        return
